// Copyright 2012 Dave Ruest.  All rights reserved.

/**
 * @fileoverview A utility to simplify drawing specific tiles from a "tiled" image.
 * Encapsulates the image creation and most of the 9 parameters to drawImage.
 * Does *not* handle image load success or error, clients will have to set these
 * handlers directly with grid.image. 
 * 
 * Defined in its own file as it is used in several places. Requires a tile size parameter...
 * might be less error prone to calculate this but that would require a loaded image
 * and *that* is more complicated than necessary.
 * @author Dave Ruest dave.ruest@gmail.com
 */

/**
 * ImageGrid constructor.
 * @param {Context} graphics The 2d graphics context of a canvas element.
 * @param {String} source the path to an image with several smaller tiled images.
 * @param {Number} size the height and width of the image tiles
 * @param {Number} rows the number of rows of tiles in the source image
 * @param {Number} columns the number of columns of tiles in the source image
 * @returns {ImageGrid} A new image grid ready to use as soon as its image is loaded.
 */
function ImageGrid(graphics, image, size, rows, columns) {
	this.graphics = graphics;
	this.image = image;
	
	this.size = size;
	this.rows = rows;
	this.columns = columns;
};

/**
 * The money method, cuts the draw parameters by more than half.
 * @param x the left side of the screen location to draw the image
 * @param y the top side of the screen location to draw the image
 * @param row vertical index of the tile to be drawn
 * @param column horizontal index of the tile to be drawn
 */
ImageGrid.prototype.draw = function(x, y, row, column) {
	var dx = column * this.size;
	var dy = row * this.size; 
	this.graphics.drawImage(this.image, dx, dy, this.size, this.size, x, y, this.size, this.size);
};
